﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
/* Index Move
 * 1./ Move Forward
 * 2./ Move Circle
 */

 
[System.Serializable]
public class ParaCircle
{
    public float angle;
    public float range;
    public float timePlusAngle;
}
public class Move : MonoBehaviour
{

    public float Speed;

    public int indexChooseMove;

   // parameters move circle
    public ParaCircle paraCircle;


    private float timeAngle;

    private Quaternion Rotation;

  //  parameters move find target
    private PlayerController player;
    private bool checkFindTarget;
    public bool checkMove5;
    public int checkIndex;
    private Benzier benzier;
    private List<Vector3> listVector;
    private float tempTime;
    private float delay;
    private float distance;
    private Vector3 V1, V2;
    
    void Awake()
    {
        Rotation = transform.rotation;
        benzier = new Benzier();
        listVector=new List<Vector3>();
        player = GameObject.FindGameObjectWithTag(Tags.player).GetComponent<PlayerController>();

    }
    void FixedUpdate()
    {

        switch (indexChooseMove)
        {
            case 1:
                // Move Forward
                rigidbody.velocity =transform.up * Speed;
                break;
            case 2:
                //Move Circle

                
                timeAngle += paraCircle.timePlusAngle;

                float temp =checkIndex*  Mathf.Clamp( Mathf.Cos(timeAngle), -paraCircle.range, paraCircle.range);

                rigidbody.velocity=new Vector3(temp*Speed, Speed,0);

                rigidbody.rotation = Quaternion.Euler(Rotation.eulerAngles.x, Rotation.eulerAngles.y, Rotation.eulerAngles.z + temp * paraCircle.angle);

                if (checkMove5)
                {
                    tempTime += Time.deltaTime;
                    if (tempTime >= 0.7f)
                        indexChooseMove = 3;
                }
                break;
            case 3:
                Quaternion rotation=   Quaternion.LookRotation(player.transform.position - transform.position);
                distance = Vector3.Distance(player.transform.position, transform.position);
             
                if (distance <= 0.5f ||player.transform.position.y>transform.position.y)
                    checkFindTarget = true;
                if (!checkFindTarget)
                {
                    if (player.transform.position.x > transform.position.x)
                    {
                       Quaternion V=Quaternion.Euler( new Vector3(0, 0, rotation.eulerAngles.y - rotation.eulerAngles.x));
                        transform.rotation = Quaternion.Slerp(transform.rotation,V,0.1f);
                    }

                    else
                    {
                        transform.rotation = Quaternion.Euler(new Vector3(0, 0, rotation.eulerAngles.y + rotation.eulerAngles.x));
                    }
                }
                else
                {
                    transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.identity, Time.deltaTime);
                }
                rigidbody.velocity = transform.up * Speed;
                break;
            case 4:
                rigidbody.velocity = transform.up * Speed;
                tempTime += Time.deltaTime;
                if (tempTime >= 2.5f && player.transform.position.y < transform.position.y)
                {
                    indexChooseMove = 3;
                    Speed = Speed * 2;
                }
                break;
            case 5:
               
                rigidbody.position = benzier._Benzier(listVector, ref tempTime, delay);
                if (tempTime >= Random.Range(0.8f,0.9f))
                    indexChooseMove = 3;
                break;
        }
    }
}
